package org.yocastlong.mybatisplus.db.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author 朱海龙
 * 2025/6/6 17:33
 */
@Slf4j
@Component
public class DateFiledHandle implements MetaObjectHandler {

    public static final String CREATE_TIME = "createTime";
    public static final String UPDATE_TIME = "updateTime";


    @Override
    public void insertFill(MetaObject metaObject) {
        if (!metaObject.hasGetter(CREATE_TIME)) {
            return;
        }
        LocalDateTime nowDateTime = LocalDateTime.now();
        metaObject.setValue(CREATE_TIME, nowDateTime);
        if (metaObject.hasGetter(UPDATE_TIME)) {
            metaObject.setValue(UPDATE_TIME, nowDateTime);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasGetter(UPDATE_TIME)) {
            LocalDateTime nowDateTime = LocalDateTime.now();
            metaObject.setValue(UPDATE_TIME, nowDateTime);
        }
    }
}
